<?php
namespace app\base\controller;
/**
 * 设置http://www.widuu.com允许跨域访问
 * header('Access-Control-Allow-Origin: http://www.baidu.com');
 * header('Access-Control-Allow-Headers: X-Requested-With,X_Requested_With');
 */
class Uploadue
{
    private $config = null;
    public function __construct()
    {
        //header('Access-Control-Allow-Origin: http://www.baidu.com');
        //header('Access-Control-Allow-Headers: X-Requested-With,X_Requested_With');
        date_default_timezone_set("Asia/Chongqing");
        error_reporting(E_ERROR);
        header("Content-Type: text/html; charset=utf-8");

        defined('DS') || define('DS', DIRECTORY_SEPARATOR);
        define('UEDITOR_PATH', ROOT_PATH.'public'.DS.'qiniu_ueditor_1.4.3'.DS.'php'.DS);

        // 注册函数方法
        spl_autoload_register(function($class){
            if( strpos(strtolower($class), "driver") ){
                $class_path = UEDITOR_PATH . 'vendor'. DS .'driver'. DS . $class. '.class.php';
            }else{
                $class_path = UEDITOR_PATH . 'vendor'. DS . $class. '.class.php';
            }

            if( file_exists($class_path) ){
                include_once($class_path);
            }else{
                return array(
                    'state' => 'ERROR',
                    'error' => $class.' not exists'
                );
            }
        });

        // php 配置信息
        $this->config = require_once( UEDITOR_PATH.'config.php' );

    }

    public function upload()
    {
        // 获取方法
        $action = !empty($_GET['action']) ? trim($_GET['action']) : '';

// 实例化处理方法
        $handle = new \Channel($this->config);

// 运行
        $response = $handle->dispatcher($action);

        $result = json_encode($response);

        /* 输出结果 */
        if (isset($_GET["callback"])) {
            if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
                echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
            } else {
                echo json_encode(array(
                    'state'=> 'callback参数不合法'
                ));
            }
        } else {
            echo $result;
        }

        exit();
    }



}


